package com.qf.controller;

import com.qf.utils.CookieUtil;
import com.qf.utils.JwtUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * @author lixu
 */
@RestController
@RequestMapping("/customer")
public class LoginController {

    public static final String REDIRECTURL = "http://localhost:10010/customer/toLogin";

    public static final String TOKEN = "token";
    /**
     * 去登录页面
     */
    @GetMapping("/toLogin")
    public String toLogin(){
        return "去登录页面";//浏览器收到此话
    }
    /**
     * 提交登录表单
     */
    @GetMapping("/login")
    public String login(String username, String password, HttpServletResponse response){
        if("zs".equals(username) && "123456".equals(password)){
            //颁发令牌
            //生成JWt
            String jwt = JwtUtil.createJWT(UUID.randomUUID().toString(), "单点登录", null);
            CookieUtil.addCookie(response,"localhost","/",TOKEN,jwt,3600000,false);
            return "登录成功";
        }else{
            return "登录失败";
        }

    }

}
